$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/category/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '一级分类名称', name: 'categoryyname', index: 'categoryyName', width: 80},
            {label: '是否新品', name: 'isnew', index: 'isnew', width: 80,formatter: function (value, options, row) {
                return value === false ?
                    '<span class="label label-danger">否</span>' :
                    '<span class="label label-success">是</span>';
            }},
            {label: '是否热卖', name: 'ishot', index: 'ishot', width: 80,formatter: function (value, options, row) {
                return value === false ?
                    '<span class="label label-danger">否</span>' :
                    '<span class="label label-success">是</span>';
            }},
            {label: '创建时间', name: 'createdate', index: 'createDate', width: 80},
            {label: '排序号', name: 'sortno', index: 'sortNo', width: 80},
            {label: '图片地址', name: 'bannerpic', index: 'bannerPic', width: 80},
            {label: '说明', name: 'tips', index: 'tips', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.records",
            page: "page.current",
            total: "page.pages",
            records: "page.total"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            username: null
        },
        showList: true,
        title: null,
        category:{
            ishot: true,
            isnew: true,
            bannerpic:null
        }
    },
    methods: {
        changeImage: function(e) {
            var file = e.target.files[0]
            var reader = new FileReader()
            var THIS = this
            reader.readAsDataURL(file)
            reader.onload = function (e) {
                // this.result 为图片
                THIS.upload(this.result)
            }
        },
        upload: function(data) {
            // 上传图片
            var THIS = this;
            if(this.$refs.file.files.length !== 0) {
                var image = new FormData()
                image.append('file', this.$refs.file.files[0])

                $.ajax({
                    url: '/liquor/uploadimage3',
                    data: image,
                    // headers: {
                    //     "Content-Type": "multipart/form-data"
                    // },
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'post',
                    success: function(result) {
                        THIS.category.bannerpic = result.address
                    },
                    error: function(error) {
                        alert('图片上传失败!');
                    }
                })
            }
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.category.bannerpic=null;
            vm.category.categoryyname=null;
            vm.category.sortno=null;
            vm.category.id=null;
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.category.id == null ? "sys/category/save" : "sys/category/update";
            vm.category.isdelete = Boolean(vm.category.isdelete);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.category),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/category/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/category/info/" + id, function (r) {
                vm.category = r.category;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'categoryyName': vm.q.categoryyname},
                page:page
            }).trigger("reloadGrid");
        }
    }
});